/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bullionxpress.client.view;

import com.bullionxpress.client.application.ClientGlobals;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class ProgressDialog extends JDialog {

    final LoaderPanel lp = new LoaderPanel();
    Timer timer = new Timer();
    ChartDataLoader loader = null;

    public ProgressDialog(JFrame owner, ChartDataLoader r) {
        super(owner, true);
        try {
            loader = r;
            this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            this.setUndecorated(true);
            //setTitle("Please wait...");
            setSize(new Dimension(375, 50));
            setContentPane(lp);
            //loader.setDialog(this);
            //loader.start();
            final ProgressDialog pd = this;
            TimerTask task = new TimerTask() {

                public void run() {
                    try {
                        JProgressBar bar = lp.getProgressBar();
                        int v = bar.getValue();
                        v++;
                        if (v > bar.getMaximum()) {
                            v = 0;
                        }
                        bar.setValue(v);
                        //System.out.println("m");
                        lp.repaint();

                        if( loader.isComplete() ){
                            ClientGlobals.cm.schedule();
                            timer.cancel();
                            pd.dispose();
                            //timer.cancel();
                        }

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            };

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = getSize();

            if (frameSize.height > screenSize.height) {
                frameSize.height = screenSize.height;
            }

            if (frameSize.width > screenSize.width) {
                frameSize.width = screenSize.width;
            }


            timer.scheduleAtFixedRate(task, 0, 100);

            setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            //setVisible(true);
        } catch (Exception ex) {
        }
    }
}
